package com.ardf.controller;

import com.ardf.common.CodeEnum;
import com.ardf.common.ReturnResult;
import com.ardf.entity.Grade;
import com.ardf.entity.dto.GradeTableDto;
import com.ardf.entity.dto.StartGamePersonTableDto;
import com.ardf.entity.dto.StartGameRelayTableDto;
import com.ardf.entity.vo.EditGradeVo;
import com.ardf.service.IGradeService;
import com.ardf.service.IResultService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ShiYunHao
 * @since 2025-01-06
 */
@RestController
@RequestMapping("/ardf/grade")
public class GradeController {
    private final IGradeService gradeService;

    @Resource
    private IResultService iResultService;

    public GradeController(IGradeService gradeService) {
        this.gradeService = gradeService;
    }

    /**
     * 成绩计算分页
     */
    @GetMapping("/searchAll")
    public ResponseEntity<Page<GradeTableDto>> searchAll(@RequestParam("pageSize") Integer pageSize,
                                                         @RequestParam("pageNum") Integer pageNum,
                                                         @RequestParam("competitionId") Long competitionId,
                                                         @RequestParam(value = "projectId", required = false) Long projectId,
                                                         @RequestParam(value = "isLastResultDecided", required = false) Boolean isLastResultDecided) {
        Page<GradeTableDto> page;
        page = gradeService.searchAll(pageSize, pageNum, competitionId, projectId, isLastResultDecided);
        return ResponseEntity.ok(page);
    }

    /**
     * 开始比赛个人分页
     */
    @GetMapping("/startGameSearchAllPerson")
    public ResponseEntity<Page<StartGamePersonTableDto>> startGameSearchAllPerson(@RequestParam("pageSize") Integer pageSize,
                                                                                  @RequestParam("pageNum") Integer pageNum,
                                                                                  @RequestParam("projectId") Long projectId,
                                                                                  @RequestParam(value = "isTerminal", required = false) Boolean isTerminal,
                                                                                  @RequestParam(value = "groupId", required = false) Long groupId) {
        return ResponseEntity.ok(
                gradeService.startGameSearchAllPerson(pageSize,pageNum,projectId,isTerminal,groupId));
    }

    /**
     * author:容伟洪
     * time:2025-3-6
     * description：查询个人赛某条具体的成绩信息,涵盖多种其他信息
     */
    @GetMapping("/startGameSearchOnlyPerson")
    public ResponseEntity<ReturnResult> startGameSearchOnlyPerson(@RequestParam("gradeId") Long gradeId) {
        StartGamePersonTableDto startGamePersonTableDto = gradeService.startGameSearchOnlyPerson(gradeId);
        ReturnResult result=new ReturnResult(CodeEnum.SUCCESS);
        result.setData(startGamePersonTableDto);
        return ResponseEntity.ok(result);
    }

    /**
     * author:容伟洪
     * time:2025-3-6
     * description：查询接力赛赛某条具体的成绩信息,涵盖多种其他信息
     */
    @GetMapping("/startGameSearchOnlyTeam")
    public ResponseEntity<ReturnResult> startGameSearchOnlyTeam(@RequestParam("gradeId") Long gradeId) {
        StartGameRelayTableDto startGamePersonTableDto = gradeService.startGameSearchOnlyTeam(gradeId);
        ReturnResult result=new ReturnResult(CodeEnum.SUCCESS);
        result.setData(startGamePersonTableDto);
        return ResponseEntity.ok(result);
    }


    /**
     * 开始比赛接力分页
     */
    @GetMapping("/startGameSearchAllRelay")
    public ResponseEntity<Page<StartGameRelayTableDto>> startGameSearchAllRelay(@RequestParam("pageSize") Integer pageSize,
                                                                                @RequestParam("pageNum") Integer pageNum,
                                                                                @RequestParam("projectId") Long projectId,
                                                                                @RequestParam(value = "isTerminal", required = false) Boolean isTerminal,
                                                                                @RequestParam(value = "groupId", required = false) Long groupId) {
        return ResponseEntity.ok(
                gradeService.startGameSearchAllRelay(pageSize,pageNum,projectId,isTerminal,groupId));
    }
    /**
     * 修改成绩信息
     */
    @PostMapping("/editInfo")
    public void editInfo(@RequestBody EditGradeVo editGradeVo){
        Grade grade = new Grade();
        BeanUtils.copyProperties(editGradeVo,grade);
        grade.setId(editGradeVo.getGradeId());
        gradeService.updateById(grade);
        //查询算法id
        List<Long> alIds = iResultService.selectByGradeId(grade.getId());
        //删除该成绩所在组比赛项目的成绩计算结果
        for (Long id :alIds){
            iResultService.deleteByAlgorithmId(id);
        }

    }

}
